package cn.gson.springboot.controller;

import cn.gson.springboot.model.pojos.Employee;
import cn.gson.springboot.model.pojos.InvoiceRecord;
import cn.gson.springboot.model.service.InvoiceRecordService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class InvoiceRecordController {
    @Autowired
    InvoiceRecordService invoiceRecordService;
    @Autowired
    HttpSession session;
    //分页查询所有的开票记录
   /* @RequestMapping("/emp-invoicerecord-list")
    @ResponseBody
    public Map<String,Object> InvoiceRecordList(Integer page, Integer size){
        Employee emp = (Employee) session.getAttribute("emp");
        Page<Object> obj = PageHelper.startPage(page, size);
        List<InvoiceRecord> invoiceRecords = invoiceRecordService.selectEmpInvoiceRecord(emp.getEmpId());
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",invoiceRecords);
        return map;
    }*/
    @RequestMapping("/emp-invoicerecord-list")
    @ResponseBody
    public Map<String,Object> InvoiceRecordList(Integer page, Integer size,Integer value,Integer num,Integer vague,String content) {
        Employee emp = (Employee) session.getAttribute("emp");
        Page<Object> obj = PageHelper.startPage(page, size);
        List<InvoiceRecord> inv = invoiceRecordService.selectEmpInvoiceRecord(emp.getDepartment().getDeptId(),emp.getEmpId(),value,num,vague,content);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",inv);
        return map;
    }
    //按票据类型查询
    @RequestMapping("/emp-invoicerecord-type-list")
    @ResponseBody
    public Map<String,Object> InvoiceRecordByTypeList(Integer page, Integer size,Integer invoicerecordType){
        Employee emp = (Employee) session.getAttribute("emp");
        Page<Object> obj = PageHelper.startPage(page, size);
        List<InvoiceRecord> invoiceRecords = invoiceRecordService.selectEmpInvoiceRecordByType(emp.getDepartment().getDeptId(),invoicerecordType);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",invoiceRecords);
        return map;
    }
    //按回款查询
    @RequestMapping("/emp-invoicerecord-back-list")
    @ResponseBody
    public Map<String,Object> InvoiceRecordByTypeListByBack(Integer page, Integer size,Integer invoicerecordBack){
        Employee emp = (Employee) session.getAttribute("emp");
        Page<Object> obj = PageHelper.startPage(page, size);
        List<InvoiceRecord> invoiceRecords = invoiceRecordService.selectEmpInvoiceRecordByBack(emp.getDepartment().getDeptId(),invoicerecordBack);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",invoiceRecords);
        return map;
    }
    //按开票日期查询
    @RequestMapping("/emp-invoicerecord-date-list")
    @ResponseBody
    public Map<String,Object> InvoiceRecordByTypeListByDate(Integer page, Integer size,String invoicerecordDate){
        Employee emp = (Employee) session.getAttribute("emp");
        Page<Object> obj = PageHelper.startPage(page, size);
        List<InvoiceRecord> invoiceRecords = invoiceRecordService.selectEmpInvoiceRecordByDate(emp.getDepartment().getDeptId(),invoicerecordDate);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",invoiceRecords);
        return map;
    }
    //按发票号码查询
    @RequestMapping("/emp-invoicerecord-number-list")
    @ResponseBody
    public Map<String,Object> InvoiceRecordByTypeListByNumber(Integer page, Integer size,String invoicerecordNo){
        Employee emp = (Employee) session.getAttribute("emp");
        Page<Object> obj = PageHelper.startPage(page, size);
        List<InvoiceRecord> invoiceRecords = invoiceRecordService.selectEmpInvoiceRecordByNumber(emp.getDepartment().getDeptId(),invoicerecordNo);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",invoiceRecords);
        return map;
    }
    //按客户名称查询
    @RequestMapping("/emp-invoicerecord-cusName-list")
    @ResponseBody
    public Map<String,Object> InvoiceRecordByTypeListByCusName(Integer page, Integer size,String customerName){
        Employee emp = (Employee) session.getAttribute("emp");
        Page<Object> obj = PageHelper.startPage(page, size);
        List<InvoiceRecord> invoiceRecords = invoiceRecordService.selectEmpInvoiceRecordByCusName(emp.getDepartment().getDeptId(),customerName);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",invoiceRecords);
        return map;
    }

    //按合同订单id查询
    @RequestMapping("back-invoice-con-list")
    @ResponseBody
    public List<InvoiceRecord> backInvoiceDateList(Integer conId){
        List<InvoiceRecord> inv = invoiceRecordService.selectBackInvoiceByConId(conId);
        return inv;
    }

    //新增与修改
    @RequestMapping("/save-invoicerecord")
    @ResponseBody
    public void saveInvoiceRecord(@RequestBody InvoiceRecord invoiceRecord ){
        invoiceRecordService.saveInvoiceRecord(invoiceRecord);
    }
    //删除
    @RequestMapping("/delete-invoicerecord")
    @ResponseBody
    public void deleteInvoiceRecord(@RequestParam(value = "invoicerecordId") Integer invoicerecordId){
        invoiceRecordService.deleteInvoiceRecord(invoicerecordId);
    }
}
